#include<time.h>
#include<stdlib.h>
#include "../Bidang/Bidang.h"

Bidang::Bidang(const int n,Point* P, char cont){
    srand(time(NULL));

    while (cont == '*'){
        cont=char(rand()%256);
    }       
    
    content = cont;
            
    if(n==2){
        nPoint=4;
        Sudut = new Point[4];
        Sudut[0].setPoint(P[0].getAbsis(),P[0].getOrdinat());
        Sudut[1].setPoint(P[1].getAbsis(),P[0].getOrdinat());
        Sudut[2].setPoint(P[1].getAbsis(),P[1].getOrdinat());
        Sudut[3].setPoint(P[0].getAbsis(),P[1].getOrdinat());
        if(P[0].getAbsis()<P[1].getAbsis()){
            minAbsis=P[0].getAbsis();
            maxAbsis=P[1].getAbsis();
        }
        else{
            minAbsis=P[1].getAbsis();
            maxAbsis=P[0].getAbsis();
        }
        if(P[0].getOrdinat()<P[1].getOrdinat()){
            minOrdinat=P[0].getOrdinat();
            maxOrdinat=P[1].getOrdinat();
        }
        else{
            minOrdinat=P[1].getOrdinat();
            maxOrdinat=P[0].getOrdinat();
        }
    }
    else{
        nPoint=n;
        Sudut = new Point[n];
        Sudut[0]=P[0];
        minAbsis=maxAbsis=P[0].getAbsis();
        minOrdinat=maxOrdinat=P[0].getOrdinat();
        for(int i=1;i<nPoint;++i) {
            Sudut[i]=P[i];
            if(minAbsis>P[i].getAbsis()) minAbsis=P[i].getAbsis();
            if(maxAbsis<P[i].getAbsis()) maxAbsis=P[i].getAbsis();
            if(minOrdinat>P[i].getOrdinat()) minOrdinat=P[i].getOrdinat();
            if(maxOrdinat<P[i].getOrdinat()) maxOrdinat=P[i].getOrdinat();	
        }
    }
}

Bidang::Bidang(const Bidang& X){
    nPoint= X.nPoint;
    content= X.content;
    minAbsis=X.minAbsis;
    maxAbsis=X.maxAbsis;
    minOrdinat=X.minOrdinat;
    maxOrdinat=X.maxOrdinat;
    Sudut = new Point[nPoint];
    for(int i=0;i<nPoint;++i) {
        Sudut[i] =X.Sudut[i];
    }
}

Bidang& Bidang::operator=(const Bidang& X){
    delete[] Sudut;
    nPoint=X.nPoint;
    content=X.content;
    minAbsis=X.minAbsis;
    maxAbsis=X.maxAbsis;
    minOrdinat=X.minOrdinat;
    maxOrdinat=X.maxOrdinat;
    Sudut = new Point[nPoint];
    for(int i=0;i<nPoint;++i) {
        Sudut[i]=X.Sudut[i];
    }
    return *this;
}
 
Bidang::~Bidang(){
    delete[] Sudut;
}

ostream& operator<< (ostream& stream, const Bidang& B){
	if(B.nPoint<2) stream << "Bidang tidak ada";
    else{
        stream << "cont: " << B.content << endl;
        stream << '(';
        for(int i=0;i<B.nPoint;++i){
            stream << B.Sudut[i];
            if(i<B.nPoint-1) stream << ',';
        }
        stream << ')';
    }      
}

int Bidang::GetN(){
    return nPoint;
}
 
char Bidang::GetContent(){
    return content;
}
 
Point& Bidang::GetSudutN(int n){
    return Sudut[n];
}

bool Bidang::inLatar(int a, int b, int c, int d){
    if((minAbsis<a)||(maxAbsis>b)||(minOrdinat<c)||(maxOrdinat>d)) return false;
    return true;
    }

void Bidang::Sweep(){
    nPoint=0;
    content='*';
    for(int i=0;i<nPoint;++i) {
        Sudut[i].setPoint(0,0);
    }
}

void Bidang::Move(int x, int y){
    for(int i=0;i<nPoint;++i) {
        Sudut[i].setPoint(Sudut[i].getAbsis()+x,Sudut[i].getOrdinat()+y);
    }
}

/*Bidang& Bidang::Mirror(const Point P1,const Point P2) {
    Bidang temp;
    for(int i=0;i<nPoint;++i) temp.Sudut[i]=Sudut[i].Mirror(P1,P2);
    return temp;
}*/
